package com.xplay.xpocker.room.mahjong;

import com.xplay.xpocker.config.security.CurrentUser;
import com.xplay.xpocker.constant.ConstantDict;
import com.xplay.xpocker.constant.RoomTypeParent;
import com.xplay.xpocker.dvo.AbstractRoom;
import com.xplay.xpocker.dvo.GameRuMahjongUserState;
import com.xplay.xpocker.entity.GameRuMahjongLogs;
import com.xplay.xpocker.room.RoomInfoTL;
import com.xplay.xpocker.room.mahjong.dto.*;
import com.xplay.xpocker.room.message.AbstractActionStrategy;
import com.xplay.xpocker.service.GameRuMahjongLogsService;
import com.xplay.xpocker.service.gaming.GameRuMahjongService;
import com.xplay.xpocker.service.gaming.GameRuMahjongUserService;
import com.xplay.xpocker.util.Assertion;

import com.xplay.xpocker.util.JacksonStringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

@Service
public class MahjongActionHandler extends AbstractActionStrategy<AbstractRoom<MahjongRoomRule, MahjongRoomInfo,GameRuMahjongLogs>, MahjongMessage> {
    @Autowired
    private GameRuMahjongUserService mahjongUserService;

    @Autowired
    private GameRuMahjongService gameRuMahjongService;
    @Autowired
    private GameRuMahjongLogsService logsService;
    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;


    @Override
    protected int strategyName() {
        return RoomTypeParent.Mahjong;
    }

    @Override
    public void doMessage(AbstractRoom<MahjongRoomRule, MahjongRoomInfo,GameRuMahjongLogs> roomInfo, MahjongMessage messageInfo) {
        if (messageInfo == null || messageInfo.getType() == null) {
            return;
        }
        // 消息是谁 发出的  如果没有值  赋值默认
        if (messageInfo.getMessageFromUserId() == null) {
            messageInfo.setMessageFromUserId(CurrentUser.getUserId());
        }
        AbstractMahjongMessageStrategy strategy = AbstractMahjongMessageStrategy.messageHandler.get(messageInfo.getType());
        Assertion.isNull(strategy, "请等待其它人的操作");
        roomInfo.getGameInfo().reInitUser(messageInfo.getMessageFromUserId());
        GameRuMahjongUserState currentUser = roomInfo.getGameInfo().getCurrentUser();
        Assertion.isTrue(currentUser.getWaitCompleted() == null &&
                currentUser.getCompleted() == null, "请等待其它人的操作");
        boolean existsWaitCompleted = currentUser.getWaitCompleted() != null && currentUser.getWaitCompleted().contains(messageInfo.getType());
        boolean existsCompleted = currentUser.getCompleted() != null && currentUser.getCompleted().contains(messageInfo.getType());
        // 如果 等待处理任务  和  正在处理任务 都不包含  属于非法行为
        if (!existsWaitCompleted && !existsCompleted) {
            throw new RuntimeException("请等待其它人的操作");
        }
        GameRuMahjongLogs.GameRuMahjongLogsBuilder logsBuilder = GameRuMahjongLogs.builder()
                .type(ConstantDict.MahjongLogType.USER).action(messageInfo.getType())
                .data(JacksonStringUtil.objectToString(messageInfo, false))
                .roomId(roomInfo.getId())
                .sourceUserId(currentUser.getUserId());
        RoomInfoTL.addLog(logsBuilder.build());
        currentUser.setWaitCompleted(null);
        currentUser.setBeforeCompleted(messageInfo.getType());
        // 记录用户历史行为
        currentUser.appendCompletedHistory(messageInfo.getType());
        //  由策略类返回 决定是否刷新房间状态   具体的事件处理类对房间信息修改
        boolean updateHome = strategy.doMessage(roomInfo, messageInfo);
        MahjongRoomInfo gameInfo = roomInfo.getGameInfo();
        // 是否是静默行为 是否客户端 匹配版本
        if (updateHome) {
            gameInfo.setSyncVersion(gameInfo.getSyncVersion() + 1);
        }
        // 持久化房间状态
        gameRuMahjongService.updateRoomInfo(roomInfo);
    }


}
